package com.hqj.demo01;

import java.util.Arrays;
import java.util.Random;

/**
 * 方法的使用-练习04
 * @author HQJ
 * 写一个扑克游戏
 *   1、创建一副扑克
 *      包含4中花色和王：黑桃，红桃，梅花，方块，王
 *      包括点数：A ,2-10,J,Q,K
 *
 *   2、扑克可以随机洗牌，发牌（player[]，card[]）Play[] player
 *
 *   3、创建3个玩家，派牌
 *
 *   4、输出三个玩家的手中牌
 * 要求：
 * 有扑克牌类Card：
 *   有private的属性String [] ranks，String [] suits，int suit，String rank
 *   ranks为牌的点数，如A,2，3，4…………，K;
 *   rank为牌的点数(单个的)
 *   suits为花色集 如（0-黑桃，1-红桃，2-梅花，3-方块，4-王）
 *   suit为花色(单个的)
 *
 *   1、有构造方法实现对点数的初始化
 *    2、提供信息输出方法toString
 *
 *   3、提供属性的get/set方法
 *
 * 有打牌者类Player:
 *   有private的属性name，card[]
 *   其中card为被派送到的牌
 *   1、有构造方法对属性进行初始化
 *   2、有添加牌的方法addCard(),参数card
 *   3、有输出打牌者信息方法toString
 *   4、提供属性的get/set方法
 *
 * 有操作员类Operator：
 *   1、有生成一副牌方法createCard，参数为num（这副牌需要多少牌），并且返回生成好的这副牌
 *   2、有洗牌方法washCard(),参数为一副Card,并且返回洗好的这副牌
 *    3、有发牌方法sendCard（），参数为play[],card[]
 *   play为打牌者人数
 *   card为牌数
 *
 * 有测试类，
 *   1、对上面进行的功能进行测试
 */
public class Exercise04 {
    public static void main(String[] args) {
        Operator o = new Operator();
        Card cards[] = o.createCard(54);
        int i = 1;
        for(Card c:cards){
            System.out.print((i++)+":"+c+",");
        }

        o.washCard(cards);
        System.out.println("洗好后:");
        for(Card c:cards){
            System.out.print((i++)+":"+c+",");
        }

        Player player[] = new Player[3];
        for(int j=1;j<=player.length;j++){
            Player p = new Player();
            p.setName("p"+(j));
            player[j-1] = p;//添加到数组
        }

        o.sendCard(player,cards);

        for(Player p:player){
            p.showInfo();
        }
    }
}

/**
 * 扑克类
 */
class Card{
    private String [] ranks = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    private String [] suits = {"黑桃","红桃","梅花","方块","王"};
    private String rank;
    private int suit;

    public Card() {}

    public Card(String rank, int suit) {
        this.rank = rank;
        this.suit = suit;
    }

    public String[] getRanks() {
        return ranks;
    }

    public void setRanks(String[] ranks) {
        this.ranks = ranks;
    }

    public String[] getSuits() {
        return suits;
    }

    public void setSuits(String[] suits) {
        this.suits = suits;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

    public int getSuit() {
        return suit;
    }

    public void setSuit(int suit) {
        this.suit = suit;
    }

    @Override
    public String toString() {
        return this.rank+"-"+this.suits[this.suit];//A-黑桃
    }
}

class Player{
    private String name;
    private Card card[] = new Card[0];

    public void addCard(Card ...card){
        for(Card c:card){
            //数组的长度自动增长长度1
            this.card = Arrays.copyOf(this.card,this.card.length+1);
            this.card[this.card.length-1] = c;
        }
    }

    public void showInfo(){
        System.out.println("\n");
        System.out.println(this.name+"手上的牌:");
        for(Card c:this.card){
            System.out.print(c);
        }
        System.out.println("\n");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Card[] getCard() {
        return card;
    }

    public void setCard(Card[] card) {
        this.card = card;
    }
}


/**
 * 操作者
 */
class Operator{

    public Card[] createCard(int num){
        //先判断牌的数量
        if(!(num==52||num==54)){
            System.out.println("无法生成扑克!");
            return null;
        }
        Card cards[] = new Card[num];//声明数组
        Card c = new Card();
        String ranks[] = c.getRanks();//点数数组
        String suits[] = c.getSuits();//花色数组
        int j = 0;
        for(String rank:ranks){//获取每一个点数
            for(int i=0;i<suits.length;i++) {
                if(i==suits.length-1){
                    continue;
                }
                int suit = i;//每一个花色
                c = new Card(rank, suit);//生成一张牌
                cards[j] = c;
                j++;
            }
        }
        if(num==54){
            cards[52] = new Card("小",4);
            cards[53] = new Card("大",4);
        }

        return cards;
    }

    /**
     * 洗牌
     * @param cards
     */
    public void washCard(Card[] cards){
        Random r = new Random();
        for(Card c:cards) {
            int index1 = r.nextInt(cards.length) + 0;
            Card card = cards[0];
            cards[0] = cards[index1];
            cards[index1] = card;
        }
    }

    /**
     * 派牌
     * @param player
     * @param card
     */
    public void sendCard(Player player[],Card card[]){
        //假设这幅牌 52张4人  打的是13张
        if(player.length==4&&card.length==52){
            //一张一人
            for(int i=0;i<card.length;i++){
                //获取每一张牌
                Card c = card[i];
                player[i%4].addCard(c);
            }
        }else if(player.length==3&&card.length==54){//斗地主
            int i=0;
            for(;i<card.length-3;i++){
                Card c = card[i];
                player[i%3].addCard(c);
            }
            //随机产生地主
            Random r = new Random();
            int index = r.nextInt(player.length);
            player[index].setName(player[index].getName()+"-地主");
            for(;i<card.length;i++) {

                player[index].addCard(card[i]);
            }
        }


    }

}













